home *** CD-ROM | disk | FTP | other *** search
- Path: news.ov.com!news
- From: glenn@ov.com (Fletcher.Glenn@ov.com)
- Newsgroups: comp.lang.c
- Subject: Re: Question - C (beginner)
- Date: 27 Feb 1996 16:32:48 GMT
- Organization: OpenVision
- Message-ID: <4gvbng$tf@spanky.pls.ov.com>
- References: <312FBA11.3946@netgate.net>
- Reply-To: glenn@ov.com
- NNTP-Posting-Host: foghorn.pls.ov.com
-
- In article 3946@netgate.net, Tamara Johnson <malihini@netgate.net> writes:
- >Problem: not correctly counting chars in
- >each word, not correctly counting words
- >with >0 && <7 chars and words with >6 chars.
- >
- >Suggestions greatly appreciated,
- >Tamara
- >
- >[stuff deleted - snippet?]
- >{
- >int i, cc, nl, nw, nc,inword;
- >
- >inword=NO;
- >cc=nl=nw=nc=0;
- >
- > for(i=0;i<n;i++)
- > {
- > nc++;
- > cc++;
-
- Above, you are counting a character before you decide that it is
- part of the word. To correct this your second "if" below should
- do cc-- to uncount the character that is not part of the word.
-
- Fletcher.Glenn@ov.com
-
- > if(test_string[i]=='\n')
- > nl++;
- > if(test_string[i]==' '||test_string[i]=='\n'
- > ||test_string[i]=='\0'||test_string[i]=='\t')
- > inword=NO;
- > else if(inword==NO)
- > {
- > inword=YES;
- > nw++;
- > if((cc>0)&&(cc<7)) under++;
- > if(cc>6) over++;
- > cc=0;
- > }
- > }
- >}
- >
- >/* -----output-------
- >
- >Enter test string: tickle something ugly
- >Number of words in the string: 3
- >Number of words with 7 or more char: 2
- >Number of words with 6 or less char: 1
- >*/
- >--
- >/* malihini@netgate.net */
- >/* a.k.a. johnson_tamara@tandem.com */
- >/* I used to think I knew logic, */
- >/* then I found C. */
-
-
-
-
-
-